
const app = getApp()
Page({
  data: {
  	phone:'',
  	address:'',
  	info:'',
  	showturnover: "#539FFF",
    showservice: "#666666",
    showprofit: "#666666",
    year: new Date().getFullYear()+"",      // 年份
    month: new Date().getMonth() + 1<10?"0"+(new Date().getMonth()+1):new Date().getMonth() + 1+'',    // 月份
    day: new Date().getDate()+"",
    startTime:null,
    endTime:null,
    days_style:[],
    calendarShow:false,
    prev: false,
    next: false,
    accountId:'',
    type:0,
    agentId:'',
    imageUrl:'',
    id:'',
    imgId:''
  },
  onLoad: function (options) {
  	app.getMemInfo().then(res=>{
  		this.setData({
  			agentId:res.data.agentId
  		})
  	})
  	this.setData({
  		imageUrl:options.url,
  		id:options.id=="null"?'':options.id,
      imgId:options.imgId
  	})
  	this.dayChoose()
  },
  onShow: function () {

  },
  showturnover: function(e) {
    this.setData({
      showturnover: "#539FFF",
      showservice: "#666666",
      showprofit: "#666666",
      isProfit:false
    })
  },
  showprofit: function(e) {
    this.setData({
      showturnover: "#666666",
      showservice: "#666666",
      showprofit: "#539FFF",
      isProfit:true
    })
  },
  //calendar
  //点击日历事件
  dayClick(e) {
    console.log(e)
    this.setData({
      allConfirm:false,
      year:e.detail.year+'',
      month:e.detail.month<10?'0'+e.detail.month:''+e.detail.month,
      day:e.detail.day+''
    })
    this.dayChoose()
  },
  //日期选择
  dayChoose(){
    let days_style=[]
    days_style.push({
      month:'current',
      day:this.data.day,
      color:'#fff',
      background:'#539FFF'
    })
    this.setData({
      days_style
    })
  },
  //点击打开日历
  bindTimeCheck(e){
    this.setData({
    	type:e.currentTarget.dataset.type,
    	calendarShow:true
    })
  },
  //日历取消按钮事件
  bindCancel(e){
    this.setData({
      calendarShow:false
    })
  },
  //日历确定按钮事件
  bindConfirm(){
    this.setData({
      calendarShow:false
    })
    const [year,month,day] = [this.data.year,this.data.month,this.data.day]
    if (this.data.type==0) {
    	this.setData({
    		startTime:year+'-'+month+'-'+day
    	})
    }else{
    	const t1 = new Date(Date.parse(this.data.startTime));
    	const t2 = new Date(Date.parse(year+'-'+month+'-'+day));
     	if (t1>t2) {
     		app.errorMsg('结束时间请勿小于开始时间')
     	}else{
	    	this.setData({
	    		endTime:year+'-'+month+'-'+day
	    	})
     	}

    }
  },
  getPhone(e){
  	this.setData({
  		phone:e.detail.value
  	})
  },
  getAddress(e){
  	this.setData({
  		address:e.detail.value
  	})
  },
  getInfo(e){
  	this.setData({
  		info:e.detail.value
  	})
  },
  save(){
  	if (this.data.phone=='') {
  		app.errorMsg('请填写联系电话')
  	}else if (this.data.address=='') {
  		app.errorMsg('请填写门店地址')
  	}else{
	  	const data = {
	  		id:this.data.id,
        posterImageId:this.data.imgId,
	      imageUrl:this.data.imageUrl,
	      phone:this.data.phone,
	      address:this.data.address,
	      startTimeStr:this.data.startTime==null?'':this.data.startTime,
	      endTimeStr:this.data.endTime==null?'':this.data.endTime,
	      content:this.data.info,
	      agentId:this.data.agentId
	  	}
	  	console.log(data)
	  	app.request('wx_app/poster/save',data,'GET').then(res=>{
	  		console.log(res)
	  		if (res.code==1) {
	  			wx.navigateBack()
	  		}else{
          app.errorMsg(res.message)
	  		}
	  	})
  	}
  }
})